﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Kardex.Modify
{
    public class PresenterSecondStepForModifyKardex
    {
        private ISecondStepForModifyKardex view;

        public PresenterSecondStepForModifyKardex(ISecondStepForModifyKardex View)
        {
            view = View;
        }

        public void ChargeDataKardex()
        {
            view.KardexToModify.Allotment = new List<Model.Allotment>{ 
                
            new Model.Allotment()
            {
                AllotmentId = view.AllotmentNameId,
                AllotmentName=view.AllotmentName,
                SquareId=view.SquareId,
            }
            };
        }

        public void RefreshSquare()
        {

            view.setListSquaresAvailable(ManagerSquare.GetSquareNotAssignedWithAssignedToKardex(view.KardexToModify));
        }

        public void SelectedSquare()
        {

            if (!string.IsNullOrEmpty(view.SquareId))
            {
                var q = ManagerAllotment.GetAllotmentNotAssignedInSquareWithAssignedToKardex(new Model.Square() { SquareId = view.SquareId },view.KardexToModify);
                if (q.Count != 0)
                {
                    view.EnableAllotmentName = true;
                    view.setListAllotmentName(q);
                }
                else
                {
                    view.SquareId = null;
                    view.EnableAllotmentName = false;
                    view.ErrorMessage = "El Manzano seleccionado no tiene lotes sin asignar porfavor seleccione otro Manzano";
                }
            }
        }

        public void UpdateDataKardex()
        {
            view.PersonId = view.KardexToModify.Person.PersonId;
            if (view.KardexToModify.Allotment != null)
                if (view.KardexToModify.Allotment.Count != 0)
                {
                    RefreshSquare();
                    view.SquareId = view.KardexToModify.AllotmentModel.SquareId;
                    SelectedSquare();
                    view.AllotmentName = view.KardexToModify.AllotmentModel.AllotmentName;
                    view.EnableAllotmentName = true;
                }
        }
    }
}
